<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="耗材名称">
          <el-input v-model="searchForm.materialName" clearable> </el-input>
        </el-form-item>
        <el-form-item label="耗材单位">
          <el-select v-model="searchForm.unitName" placeholder="请选择">
            <el-option
              v-for="item in [
                '包',
                '桶',
                '袋',
                '盒',
                '箱',
                '个',
                '瓶',
                '片',
                '根',
                '双',
                '卷',
                '支',
                '把',
                '套',
                '件',
              ]"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button"
            >查询</el-button
          >
          <el-button @click="handleReset" class="search-button">重置</el-button>
          <el-button type="primary" @click="verify">确认</el-button>
          <el-button type="primary" @click="close">返回</el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="table"
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column
          align="center"
          type="selection"
          label="序号"
          width="55"
        >
        </el-table-column>
        <el-table-column label="耗材名称" align="center" prop="materialName">
        </el-table-column>
        <el-table-column label="耗材图片" align="center" prop="materialPic">
          <template slot-scope="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.materialPic"
              :preview-src-list="[row.materialPic]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="耗材单位" align="center" prop="unitName">
        </el-table-column>
        <el-table-column label="参考价格（元)" align="center" prop="price">
        </el-table-column>
        <el-table-column label="单价（元）" align="center" prop="inputPrice">
        </el-table-column>
        <el-table-column label="数量" align="center" prop="number">
          <template slot-scope="{ row }">
            <el-input-number
              v-model="row.number"
              :min="0"
              size="mini"
              clearable
            >
            </el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="规格" align="center" prop="specs">
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchForm.pageIndex"
        :limit.sync="searchForm.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import * as consumable from '@/api/consumable'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      param.hospitalUid = localStorage.getItem('hospitalUid')
      consumable.queryByPage(param).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })
    },
    handleFilter() {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset() {
      this.resetTemp();
      this.getList();
    },
    resetTemp() {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    verify() {
      let arr = this.$refs.table.selection
      if (arr.length === 0) {
        this.$message.error('请勾选数据！')
      } else {
        this.$emit('close', arr)
      }
    },
    close() {
      this.$emit('close', '')
    }
  }
}
</script>
